package cn.jsj.hzu.qm.facecloud.ciuser.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.edu.hzu.jsj.utils.BeanFactory;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Ciuser;
import cn.jsj.hzu.qm.facecloud.ciuser.service.CiuserService;
import cn.jsj.hzu.qm.facecloud.utils.FileProcesser;

/**
 * Servlet implementation class CiuserFileUploadServlet
 */
public class CiuserFileUploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		CiuserService ciuserService = (CiuserService) BeanFactory.getBean("ciuserService");

		FileProcesser fileProcesser = new FileProcesser(request.getInputStream());
		
		LinkedList<LinkedList<Integer>> list = fileProcesser.getSplitBySymble();
		/*
//JS:		
//		formData = new FormData();
//		formData.append("photo", $myfile[0].files[0]);
//		formData.append("message", '<%=stuId%>');
//		formData.append("A", 'A');
//		formData.append("B", 'B');
		*/
		
		//参数数量不匹配
		if (list.size() != (4*2)+1) {
			response.getWriter().write("Count error "+list.size()+"(9)");
			return;
		}

		//获取id
		LinkedList<Integer> stuIdLinkedList = list.get(3);
		StringBuilder stuIdBuilder = new StringBuilder();
		//注意，需要去掉末尾4个标识符
		for(int i = 0; i < stuIdLinkedList.size()-4; i++) {
			stuIdBuilder.append((char)((int)stuIdLinkedList.get(i)));
		}
		System.out.println(stuIdLinkedList);
		String stuIdString = stuIdBuilder.toString();
		System.out.println(stuIdString);
		int ciId = Integer.parseInt(stuIdString);
		
		Ciuser ciuser = ciuserService.findCiuserById(ciId);
		if (ciuser == null) {
			response.getWriter().write("{'state':'id not found'}");
			return;
		}
		
		//获取图片
		LinkedList<Integer> photoLinkedList = list.get(1);
		
		String path = this.getServletContext().getRealPath("/ciface");
		System.out.println("666->"+path);
		Date date = new Date();
		String jpgFileNameString = "rawface_"+ciId+"_"+date.getTime()+".jpg";
		
		
		FileOutputStream fileOutputStream = new FileOutputStream(new File(path + File.separatorChar + jpgFileNameString));
		
		for(int b:photoLinkedList) {
			fileOutputStream.write(b);
		}
		
		fileOutputStream.close();
		
		String photoUrlString = "../ciface/"+jpgFileNameString;
		ciuser.setPhotoPath(photoUrlString);
		ciuserService.modifyCiuser(ciuser);
		request.getSession().setAttribute("ciuser", ciuser);
		
		response.getWriter().write("{'state':'success', 'photourl':'"+photoUrlString+"'}");
		return;
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
